import tinycolor from 'tinycolor2'
import { transFormToUPlot } from './data-transform'

export function mockSeriesData({
  pointCount = 100,
  name,
  isArea,
  ...args
} = {}) {
  const list = []
  for (let i = 0; i < pointCount; i++) {
    const min = i === 0 ? 0 : list.slice(-1)[0].x
    const point = mockSinglePoint({ minX: min, isArea, ...args })
    list.push(point)
  }

  const color = tinycolor.random()

  return {
    isArea,
    name: name || 'seriesTitle' + Math.random(),
    data: list,
    color
  }
}

function mockSinglePoint({ minX = 0, stepX = Math.random() * 10, isArea }) {
  return {
    x: minX + stepX,
    y: Math.random() * 100,
    l: isArea ? Math.random() * 5 + 5 : null
  }
}

export function mockChartData({ seriesCount = 3, asArray, ...args } = {}) {
  if (asArray) return mockArrayChartData({ seriesCount, asArray, ...args })
  const list = []

  if (Array.isArray(seriesCount)) {
    seriesCount = Math.floor(
      Math.random() * (seriesCount[1] - seriesCount[0]) + seriesCount[0]
    )
  }

  for (let i = 0; i < seriesCount; i++) {
    args.name = 'seriesTitle' + i
    const series = mockSeriesData(args)

    list.push(series)
  }

  return list
}

export function mockArrayChartData({ seriesCount = 3, ...args } = {}) {
  const label = ['x']
  const list = []

  for (let i = 0; i < seriesCount; i++) {
    const series = mockSeriesData(args)

    label.push(series.name)

    for (const pointIndex in series.data) {
      const point = series.data[pointIndex]
      if (i === 0) {
        list.push([point.x, point.y])
      } else {
        list[pointIndex][i + 1] = point.y
      }
    }
  }

  return { label, list }
}

export function mockChartList({ chartCount = 3, ...args } = {}) {
  const list = []
  for (let i = 0; i < chartCount; i++) {
    list.push({
      title: 'chartTitle' + i,
      series: mockChartData(args)
    })
  }

  return list
}

// export function mockAreaRange() {
//   return {
//     title: 'arearange',
//     series: {
//       isArea: true,
//       list: [
//         [
//           1, []
//         ]
//       ],
//       label: []
//     }
//   }
// }

export function memorySizeOf(obj) {
  var bytes = 0

  function sizeOf(obj) {
    if (obj !== null && obj !== undefined) {
      switch (typeof obj) {
        case 'number':
          bytes += 8
          break
        case 'string':
          bytes += obj.length * 2
          break
        case 'boolean':
          bytes += 4
          break
        case 'object':
          bytes += Buffer.byteLength(JSON.stringify(obj))
          break
        default:
          break
      }
    }
    return bytes
  }

  function formatByteSize(bytes) {
    if (bytes < 1024) return bytes + ' bytes'
    else if (bytes < 1048576) return (bytes / 1024).toFixed(3) + ' KiB'
    else if (bytes < 1073741824) return (bytes / 1048576).toFixed(3) + ' MiB'
    else return (bytes / 1073741824).toFixed(3) + ' GiB'
  }

  const num = sizeOf(obj)

  return {
    text: formatByteSize(num),
    num
  }
}

// byte
export function mockBigData(size = 1024 * 1024) {
  // default 1 mb
  let data = mockChartList({
    chartCount: 1,
    seriesCount: 5,
    pointCount: 50,
    isArea: false
  })
  data = data.map(transFormToUPlot)

  const dataSize = memorySizeOf(data)

  return Array(Math.ceil(size / dataSize.num)).fill(data)
}
